<?php
/**
 * Joomla! 1.5 component JoomGPSTracks
 *
 * @version $Id: tracks.php 2009-06-24 15:40:19 svn $
 * @author Michael Pfister
 * @package joomGPStracks
 * @subpackage frontend
 * @license GNU/GPL
 * @filesource
 *
 */

// no direct access
defined('_JEXEC') or die('Restricted access');

jimport( 'joomla.application.component.controller' );

class JoomgpstracksControllerTracks extends JoomgpstracksController  {

   function __construct()
   {
           parent::__construct();
   }

    function save()  {
        jimport('joomla.filesystem.file');
        
        // Check for request forgeries
        JRequest::checkToken() or jexit( 'Invalid Token' );
        $file =& JRequest::getVar('file', null, 'files', 'array');
        $images =& $_FILES['images'];
        $model = $this->getModel('tracks');

        $ext = JFile::getExt($file['name']);
        if($ext == 'kml' || $ext == 'gpx' || $ext == 'tcx')  {
            if(!$model->saveTrack()) {
                echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
            }
            $this->setRedirect( JRoute::_('index.php?option=com_joomgpstracks'), false );

        } else {
            echo "<script> alert('".JText::_('GPS_FILE_ERROR')."'); window.history.go(-1); </script>\n";
            exit;
        }
    }

    function delete()  {
        $user		=& JFactory::getUser();

        if(!$user->get('id')):
            $this->setRedirect( JRoute::_('index.php?option=com_joomgpstracks'), false );
        endif;

        $id =& JRequest::getInt('id');
        $model = $this->getModel('tracks');

        if(!$model->deleteTrack($id))  {
            echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
        } else {
            $this->setRedirect( JRoute::_('index.php?option=com_joomgpstracks&view=tracks&layout=user'), false);
        }
    }

    function update()  {
        jimport('joomla.filesystem.file');
        $user		=& JFactory::getUser();

        if(!$user->get('id')):
            $this->setRedirect( JRoute::_('index.php?option=com_joomgpstracks'), false );
        endif;

		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );
        $id =& JRequest::getInt('id');
        $model = $this->getModel('tracks');

            if(!$model->updateTrack($id)) {
                echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
            }
            $this->setRedirect( JRoute::_('index.php?option=com_joomgpstracks'), false );

    }

    function addcomment()  {

        $model = $this->getModel('tracks');
        $model->addcomment();
    }

    function savecomment()  {
        global $mainframe;
        
        // Check for request forgeries
        JRequest::checkToken() or jexit( 'Invalid Token' );
        $cfg = joomgpstracksHelper::getConfig();
        $id = JRequest::getInt('id');
        
        if($cfg->captcha == 1)  {
            $return = false;
            $word = JRequest::getVar('word', false, '', 'CMD');
            $mainframe->triggerEvent('onCaptcha_confirm', array($word, &$return));
               if (!$return) {
                    echo "<script> alert('".JText::_('GPS_CAPTCHA_WRONG')."'); window.history.go(-1); </script>\n";
               } else {
                    $model = $this->getModel('tracks');
                    if(!$model->savecomment($id, $cfg)) {
                        echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
                    }

                    $msg = JText::_('GPS_COMMENT_SAVED');
                    $this->setRedirect( JRoute::_('index.php?option=com_joomgpstracks&view=tracks&layout=track&id='.$id), $msg );
               }
        } else {
               $model = $this->getModel('tracks');
                if(!$model->savecomment($id, $cfg)) {
                    echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
                }

                $msg = JText::_('GPS_COMMENT_SAVED');
                $this->setRedirect( JRoute::_('index.php?option=com_joomgpstracks&view=tracks&layout=track&id='.$id), $msg );
        }

    }

}
?>
